/*
 *  Copyright 2011 yuxinlei.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package camel.first.api.datastore.internal.dao.impl;

import camel.first.api.datastore.dao.PromotionBuildsEntityJpaController;
import camel.first.api.datastore.dao.exceptions.NonexistentEntityException;
import camel.first.api.datastore.dao.exceptions.PreexistingEntityException;
import camel.first.api.datastore.internal.entity.PromotionBuildsEntity;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.EntityNotFoundException;
import camel.first.api.datastore.internal.entity.PromotionsEntity;

/**
 *
 * @author yuxinlei
 */
public class PromotionBuildsEntityJpaControllerImpl implements PromotionBuildsEntityJpaController {

    public PromotionBuildsEntityJpaControllerImpl() {
        emf = Persistence.createEntityManagerFactory("reportdb");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(PromotionBuildsEntity promotionBuildsEntity) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PromotionsEntity promotionsByPromotionId = promotionBuildsEntity.getPromotionsByPromotionId();
            if (promotionsByPromotionId != null) {
                promotionsByPromotionId = em.getReference(promotionsByPromotionId.getClass(), promotionsByPromotionId.getId());
                promotionBuildsEntity.setPromotionsByPromotionId(promotionsByPromotionId);
            }
            em.persist(promotionBuildsEntity);
            if (promotionsByPromotionId != null) {
                promotionsByPromotionId.getPromotionBuildsesById().add(promotionBuildsEntity);
                promotionsByPromotionId = em.merge(promotionsByPromotionId);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findPromotionBuildsEntity(promotionBuildsEntity.getId()) != null) {
                throw new PreexistingEntityException("PromotionBuildsEntity " + promotionBuildsEntity + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(PromotionBuildsEntity promotionBuildsEntity) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PromotionBuildsEntity persistentPromotionBuildsEntity = em.find(PromotionBuildsEntity.class, promotionBuildsEntity.getId());
            PromotionsEntity promotionsByPromotionIdOld = persistentPromotionBuildsEntity.getPromotionsByPromotionId();
            PromotionsEntity promotionsByPromotionIdNew = promotionBuildsEntity.getPromotionsByPromotionId();
            if (promotionsByPromotionIdNew != null) {
                promotionsByPromotionIdNew = em.getReference(promotionsByPromotionIdNew.getClass(), promotionsByPromotionIdNew.getId());
                promotionBuildsEntity.setPromotionsByPromotionId(promotionsByPromotionIdNew);
            }
            promotionBuildsEntity = em.merge(promotionBuildsEntity);
            if (promotionsByPromotionIdOld != null && !promotionsByPromotionIdOld.equals(promotionsByPromotionIdNew)) {
                promotionsByPromotionIdOld.getPromotionBuildsesById().remove(promotionBuildsEntity);
                promotionsByPromotionIdOld = em.merge(promotionsByPromotionIdOld);
            }
            if (promotionsByPromotionIdNew != null && !promotionsByPromotionIdNew.equals(promotionsByPromotionIdOld)) {
                promotionsByPromotionIdNew.getPromotionBuildsesById().add(promotionBuildsEntity);
                promotionsByPromotionIdNew = em.merge(promotionsByPromotionIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                int id = promotionBuildsEntity.getId();
                if (findPromotionBuildsEntity(id) == null) {
                    throw new NonexistentEntityException("The promotionBuildsEntity with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(int id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PromotionBuildsEntity promotionBuildsEntity;
            try {
                promotionBuildsEntity = em.getReference(PromotionBuildsEntity.class, id);
                promotionBuildsEntity.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The promotionBuildsEntity with id " + id + " no longer exists.", enfe);
            }
            PromotionsEntity promotionsByPromotionId = promotionBuildsEntity.getPromotionsByPromotionId();
            if (promotionsByPromotionId != null) {
                promotionsByPromotionId.getPromotionBuildsesById().remove(promotionBuildsEntity);
                promotionsByPromotionId = em.merge(promotionsByPromotionId);
            }
            em.remove(promotionBuildsEntity);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }


    public PromotionBuildsEntity findPromotionBuildsEntity(int id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(PromotionBuildsEntity.class, id);
        } finally {
            em.close();
        }
    }

}
